package com.conn3cted.server;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.ajp.Ajp13SocketConnector;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StartJetty  {

    static boolean stop = false;
	
	static public void main(String[] args) { 
		Server server = new Server();
		
		SocketConnector connector = new SocketConnector();
		connector.setMaxIdleTime(1000 * 60 * 60);
		connector.setSoLingerTime(-1);
		connector.setPort(8080);
		
	
		Ajp13SocketConnector ajp = new Ajp13SocketConnector();
		ajp.setPort(8009);
		
		server.setConnectors(new Connector[] { connector, ajp});

		WebAppContext context = new WebAppContext();
		context.setServer(server);
		context.setContextPath("/phrasebook");
		context.setWar("./src/main/webapp");
		
		 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:WEB-INF/spring/webmvc-config.xml"); 
		
		server.addHandler(context);


		
		try {
			System.out.println(">>> STARTING EMBEDDED JETTY SERVER");
			server.start();
			
			while (!stop) {
				Thread.sleep(2000);
			}
			
			server.stop();
			server.join();
			
			
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}

	}


	public static void stop() {
		stop = true;
	}
	

}
